##use strict;
use HTML::TreeBuilder;
use File::Path;
use Time::Local 'timelocal';
use warnings;
use File::Spec::Functions;
use Cwd;
## Static variable Define##
my $MP_Filename ="China_Product_SW_Update_Schedule_MP.xlsx";
my $SD_Filename ="China_Product_SW_Update_Schedule_SD.xlsx";
my $currentdir= "C:\\FileChangeNotifyTool";
my $oldfilepath= "C:\\FileChangeNotifyTool\\Old_Files";
my $currentTestFilePath = "C:\\";
my $MP_Time_Stamp_path = $oldfilepath.'\\MP_Time_Stamp.txt';
my $SD_Time_Stamp_path = $oldfilepath.'\\SD_Time_Stamp.txt';
my $Old_MP_File_Path = $oldfilepath.'\\'.$MP_Filename;
my $Old_SD_File_Path = $oldfilepath.'\\'.$SD_Filename;
my $Correct_MP_File_Path = $currentTestFilePath.$MP_Filename;
my $Correct_SD_File_Path = $currentTestFilePath.$SD_Filename;
## End Static variable Define##

#************Main Scirpts*************#
print "File Modify Checking Tool Start...\n\n";

print "Start Check the MP File update:\n";
##Get the Correct MP file modify time
my $MPModifyTime =&getModifyTime($Correct_MP_File_Path);
##Get the Last MP file modify time from log file.
my $MP_Times =&getLatestModifyTime($MP_Time_Stamp_path);

if ($MPModifyTime ne $MP_Times)
{
	&insertTimeStamp($MP_Time_Stamp_path,$MPModifyTime);
	print "SD File is update, update log file and send notification mail.\n";
	##TODO Call Send Notify Mail!
	&sendModifyAlert("MP");
	
} else {
	print "MP File Modify time is the same, do not update and continue\n";
}


print "\n\nStart Check the SD File update:\n";
##Get the Correct SD file modify time
my $SDModifyTime =&getModifyTime($Correct_SD_File_Path);
##Get the Last SD file modify time from log file.
my $SD_Times =&getLatestModifyTime($SD_Time_Stamp_path);

if ($SDModifyTime ne $SD_Times)
{
	&insertTimeStamp($SD_Time_Stamp_path,$SDModifyTime);
	print "SD File is update, update log file and send notification mail.\n";
	##TODO Call Send Notify Mail!
	&sendModifyAlert("SD");
	
} else {
	print "SD File Modify time is the same, do not update and continue\n";
}



#************Main Scirpts End*************#


##Functions##########################################################

sub sendModifyAlert{
	my ($mailSelection)=@_;
	if($mailSelection eq "MP"){
	my $sendalertsuccess = $currentdir.'\send_email2.py';
	system($sendalertsuccess);
	print "MP Modify alert is sent.\n\n";
	}
	if($mailSelection eq "SD"){
	my $sendalertsuccess = $currentdir.'\send_email.py';
	system($sendalertsuccess);
	print "SD Modify alert is sent.\n\n";
	}
}


sub insertTimeStamp{
	my($update_log_path,$timestamp)=@_;
	my $temppath='>>'.$update_log_path;
	open(TS_FILE, $temppath) or die "Can't open $temppath.\n";
	print TS_FILE $timestamp;
	print TS_FILE "\n";
	close(TS_FILE);	
}

sub getModifyTime{
	my ($File_Path) = @_;
	my $L_file_modify_time ='';
	my @File_Attributes =stat($File_Path);
	if ($File_Attributes[9] eq ''){
		print "File Not found\n";
		##TODO Call warning mail
		return $L_file_modify_time;
		} else {
		$L_file_modify_time = $File_Attributes[9];
		print "File modify time stamp is $L_file_modify_time\n";
		return $L_file_modify_time;
		}
}

sub getLatestModifyTime{
	my($LogFilePath)=@_;
	open(LOG_FILE, $LogFilePath) or die "Can't open $LogFilePath\n";
	my $Last_Modify_Times ='';
	while(<LOG_FILE>)
		{
			chomp;
			#print "$_\n";
			$Last_Modify_Times = $_;
		}	
	close(LOG_FILE);
	print "Latest Log Modify file Time is $Last_Modify_Times\n";
	return $Last_Modify_Times;
}


##Functions##########################################################
